package recallin.gui;

import org.mapsforge.core.GeoPoint;

import recallin.activities.MainActivity;
import recallin.activities.PlacesActivity;
import recallin.activities.R;
import recallin.database.DatabaseUtility;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * A custom cursoradapter that handles all the sql information output to the
 * user about POI & Favorites.
 * 
 * @author philipmasek
 * 
 */

public class PlacesAdapter extends CursorAdapter {

	public PlacesAdapter(Context context, Cursor c) {
		super(context, c);
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		TextView top_row = (TextView) view.findViewById(R.id.top_row);
		TextView bottom_row = (TextView) view.findViewById(R.id.bottom_row);
		TextView bottom_row_title = (TextView) view
				.findViewById(R.id.places_title);
		ImageView favorite_image = (ImageView) view
				.findViewById(R.id.favorite_image);
		if (top_row != null) {
			top_row.setText(cursor.getString(cursor.getColumnIndex("name")));
		}
		if (bottom_row != null) {
			if (MainActivity.getCurrentLocation() != null) {
				bottom_row_title.setText("Distance:");
				bottom_row.setText(MainActivity.calculateDistance(new GeoPoint(
						cursor.getDouble(9), cursor.getDouble(8)), MainActivity
						.getCurrentLocation())
						+ " meters");
			} else
				bottom_row.setText(cursor.getString(cursor
						.getColumnIndex("street"))
						+ ", "
						+ cursor.getString(cursor.getColumnIndex("city")));
		}
		if (cursor.getString(cursor.getColumnIndex("favorite_name")) != null) {
			top_row.setText(cursor.getString(cursor
					.getColumnIndex("favorite_name")));
			favorite_image
					.setBackgroundResource(android.R.drawable.star_big_on);
		} else {
			if (cursor.getString(cursor.getColumnIndex("name")).equals("null")) {
				top_row.setText(cursor.getString(cursor.getColumnIndex("type")));
			}
			favorite_image
					.setBackgroundResource(android.R.drawable.star_big_off);
		}
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		LayoutInflater inflater = LayoutInflater.from(context);
		View v = inflater.inflate(R.layout.places_list_item, parent, false);
		bindView(v, context, cursor);
		return v;
	}

	@Override
	public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
		return filter(constraint, 10);
	}

	public Cursor filter(CharSequence constraint, int size) {
		if (constraint == null || constraint.length() == 0) {
			if (PlacesActivity.getCurrentCategory().equals("All")) {
				return DatabaseUtility.getDatabase().rawQuery(
						"SELECT * FROM places ORDER BY favorite_name DESC LIMIT "
								+ size, null);
				// SELECT * FROM PLACES ORDER BY
				// ((latitude-57.7069749)*(latitude-57.7069749)+(longitude-11.936932)*(longitude-11.936932))
			} else
				return DatabaseUtility
						.getDatabase()
						.rawQuery(
								"SELECT * FROM places WHERE category='"
										+ PlacesActivity.getCurrentCategory()
										+ "' ORDER BY favorite_name DESC LIMIT "
										+ size, null);
		} else {
			if (PlacesActivity.getCurrentCategory().equals("All")) {
				return DatabaseUtility
						.getDatabase()
						.rawQuery(
								"SELECT * FROM PLACES where (favorite_name like '"
										+ constraint
										+ "%') or (favorite_name is null and name like '"
										+ constraint
										+ "%') ORDER BY favorite_name DESC LIMIT "
										+ size, null);
			} else
				return DatabaseUtility
						.getDatabase()
						.rawQuery(
								"SELECT * FROM PLACES where (favorite_name like '"
										+ constraint
										+ "%' and category='"
										+ PlacesActivity.getCurrentCategory()
										+ "') or (favorite_name is null and name like '"
										+ constraint
										+ "%' and category='"
										+ PlacesActivity.getCurrentCategory()
										+ "') ORDER BY favorite_name DESC LIMIT "
										+ size, null);
		}
	}

}